package com.happy.code.gui;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.List;
import java.util.Vector;

import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.MatteBorder;
import javax.swing.border.TitledBorder;

import org.apache.commons.lang3.StringUtils;

import com.happy.code.bean.Config;
import com.happy.code.bean.ConfigVo;
import com.happy.code.conf.ConfigLoader;
import com.happy.code.util.DBHelper;

import net.miginfocom.swing.MigLayout;

/**
 * @类说明：
 * 
 * @author YANGJIE
 * @version 1.0
 * @创建时间：2016年8月12日 下午4:14:16
 */
public class ConfigPanel extends JPanel {

	private static final long serialVersionUID = -1943158575050485399L;
	
	private JPanel northPanel;
	private JPanel centerPanel;
	private JPanel southPanel;
	private JComboBox<ConfigVo> configComboBox;
	private JLabel comboBoxLabel;
	private JButton saveButton;
	private JButton clearButton;
	private JLabel databaseNameLabel;
	private JTextField databaseNameTextField;
	private JLabel basePackageLabel;
	private JTextField basePackageTextField;
	private JLabel beanPackageLabel;
	private JTextField beanPackageTextField;
	private JLabel daoPackageLabel;
	private JTextField daoPackageTextField;
	private JLabel mapperPackageLabel;
	private JTextField mapperPackageTextField;
	private JLabel servicePackageLabel;
	private JTextField servicePackageTextField;
	private JLabel controllerPackageLabel;
	private JTextField controllerPackageTextField;
	private JLabel entityPackageLabel;
	private JTextField entityPackageTextField;
	private JLabel enumPackageLabel;
	private JTextField enumPackageTextField;
	private JLabel tablesLabel;
	private JTextField tablesTextField;
	private JLabel needModulesLabel;
	private JTextField needModulesTextField;
	private JLabel pathLabel;
	private JTextField pathTextField;
	
	private DefaultComboBoxModel<ConfigVo> dcbm = null;
	private ConfigLoader config = ConfigLoader.getInstance();
	private JLabel configLabel;
	private JTextField configTextField;
	private JPanel panel;
	private JLabel driverLabel;
	private JTextField driverTextField;
	private JLabel urlLabel;
	private JTextField urlTextField;
	private JLabel usernameLabel;
	private JTextField usernameTextField;
	private JLabel passwordLabel;
	private JTextField passwordTextField;
	private JLabel isDefaultLabel;
	private JRadioButton isDefaultYesRadioButton;
	private JRadioButton isDefaultNoRadioButton;
	private JButton testConnButton;

	public ConfigPanel() {
		initGUI();
	}
	
	private void initGUI() {
		setLayout(new MigLayout("", "[grow]", "[][grow][grow][]"));
		
		northPanel = new JPanel();
		northPanel.setBorder(new TitledBorder(new MatteBorder(1, 0, 0, 0, (Color) new Color(128, 128, 128)), "\u9009\u62E9\u914D\u7F6E", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(192, 192, 192)));
		northPanel.setLayout(new MigLayout("", "[::96px,grow][grow]", "[]"));
		
		comboBoxLabel = new JLabel("选择配置");
		northPanel.add(comboBoxLabel, "cell 0 0,alignx trailing");
		configComboBox = new JComboBox<ConfigVo>(initCombox());
		configComboBox.addItemListener(new ConfigComboBoxItem());
		northPanel.add(configComboBox, "cell 1 0,growx");
		
		add(northPanel, "cell 0 0,grow");
		
		panel = new JPanel();
		panel.setBorder(new TitledBorder(new MatteBorder(1, 0, 0, 0, (Color) new Color(128, 128, 128)), "\u6570\u636E\u5E93\u8FDE\u63A5", TitledBorder.LEADING, TitledBorder.TOP, null, Color.LIGHT_GRAY));
		add(panel, "cell 0 1,grow");
		panel.setLayout(new MigLayout("", "[::96px,grow][grow]", "[][][][]"));
		
		driverLabel = new JLabel("数据库驱动");
		panel.add(driverLabel, "cell 0 0,alignx trailing");
		
		driverTextField = new JTextField();
		panel.add(driverTextField, "cell 1 0,growx");
		driverTextField.setColumns(10);
		
		urlLabel = new JLabel("数据库连接");
		panel.add(urlLabel, "cell 0 1,alignx trailing");
		
		urlTextField = new JTextField();
		panel.add(urlTextField, "cell 1 1,growx");
		urlTextField.setColumns(10);
		
		usernameLabel = new JLabel("数据库用户名");
		panel.add(usernameLabel, "cell 0 2,alignx trailing");
		
		usernameTextField = new JTextField();
		panel.add(usernameTextField, "cell 1 2,growx");
		usernameTextField.setColumns(10);
		
		passwordLabel = new JLabel("数据库密码");
		panel.add(passwordLabel, "cell 0 3,alignx trailing");
		
		passwordTextField = new JTextField();
		panel.add(passwordTextField, "cell 1 3,growx");
		passwordTextField.setColumns(10);
		
		centerPanel = new JPanel();
		centerPanel.setBorder(new TitledBorder(new MatteBorder(1, 0, 0, 0, (Color) new Color(128, 128, 128)), "\u53C2\u6570\u914D\u7F6E", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(192, 192, 192)));
		centerPanel.setLayout(new MigLayout("", "[::96px,grow][grow]", "[][][][][][][][][][][][][][]"));
		
		configLabel = new JLabel("配置名称");
		centerPanel.add(configLabel, "cell 0 0,alignx trailing");
		
		configTextField = new JTextField();
		centerPanel.add(configTextField, "cell 1 0,growx");
		configTextField.setColumns(10);
		
		databaseNameLabel = new JLabel("数据库名称");
		centerPanel.add(databaseNameLabel, "cell 0 1,alignx trailing");
		
		databaseNameTextField = new JTextField();
		centerPanel.add(databaseNameTextField, "cell 1 1,growx");
		
		basePackageLabel = new JLabel("项目基础包名");
		centerPanel.add(basePackageLabel, "cell 0 2,alignx trailing");
		
		basePackageTextField = new JTextField();
		centerPanel.add(basePackageTextField, "cell 1 2,growx");
		
		beanPackageLabel = new JLabel("实体目录名称");
		centerPanel.add(beanPackageLabel, "cell 0 3,alignx trailing");
		
		beanPackageTextField = new JTextField();
		centerPanel.add(beanPackageTextField, "cell 1 3,growx");
		
		daoPackageLabel = new JLabel("DAO包目录");
		centerPanel.add(daoPackageLabel, "cell 0 4,alignx trailing");
		
		daoPackageTextField = new JTextField();
		centerPanel.add(daoPackageTextField, "cell 1 4,growx");
		
		mapperPackageLabel = new JLabel("Mapper包目录");
		centerPanel.add(mapperPackageLabel, "cell 0 5,alignx trailing");
		
		mapperPackageTextField = new JTextField();
		centerPanel.add(mapperPackageTextField, "cell 1 5,growx");
		
		servicePackageLabel = new JLabel("Service包目录");
		centerPanel.add(servicePackageLabel, "cell 0 6,alignx trailing");
		
		servicePackageTextField = new JTextField();
		servicePackageTextField.setText("");
		centerPanel.add(servicePackageTextField, "cell 1 6,growx");
		
		controllerPackageLabel = new JLabel("Controller包目录");
		centerPanel.add(controllerPackageLabel, "cell 0 7,alignx trailing");
		
		controllerPackageTextField = new JTextField();
		centerPanel.add(controllerPackageTextField, "cell 1 7,growx");
		
		entityPackageLabel = new JLabel("Entity完整包名");
		centerPanel.add(entityPackageLabel, "cell 0 8,alignx trailing");
		
		entityPackageTextField = new JTextField();
		centerPanel.add(entityPackageTextField, "cell 1 8,growx");
		
		enumPackageLabel = new JLabel("Enum完整包名");
		centerPanel.add(enumPackageLabel, "cell 0 9,alignx trailing");
		
		enumPackageTextField = new JTextField();
		centerPanel.add(enumPackageTextField, "cell 1 9,growx");
		
		tablesLabel = new JLabel("需要处理的表");
		centerPanel.add(tablesLabel, "cell 0 10,alignx trailing");
		
		tablesTextField = new JTextField();
		centerPanel.add(tablesTextField, "cell 1 10,growx");
		
		needModulesLabel = new JLabel("需要处理的模块");
		centerPanel.add(needModulesLabel, "cell 0 11,alignx trailing");
		
		needModulesTextField = new JTextField();
		centerPanel.add(needModulesTextField, "cell 1 11,growx");
		
		pathLabel = new JLabel("代码生成目录");
		centerPanel.add(pathLabel, "cell 0 12,alignx trailing");
		
		pathTextField = new JTextField();
		centerPanel.add(pathTextField, "cell 1 12,growx");

		add(centerPanel, "cell 0 2,grow");
		
		isDefaultLabel = new JLabel("是否默认");
		centerPanel.add(isDefaultLabel, "cell 0 13,alignx trailing");
		
		isDefaultYesRadioButton = new JRadioButton("是");
		centerPanel.add(isDefaultYesRadioButton, "flowx,cell 1 13");
		
		isDefaultNoRadioButton = new JRadioButton("否");
		centerPanel.add(isDefaultNoRadioButton, "cell 1 13");
		
		ButtonGroup bg = new ButtonGroup();// 初始化按钮组
		bg.add(isDefaultYesRadioButton);
		bg.add(isDefaultNoRadioButton);
		
		southPanel = new JPanel();
		southPanel.setBorder(new TitledBorder(new MatteBorder(1, 0, 0, 0, (Color) new Color(128, 128, 128)), "\u64CD\u4F5C", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(192, 192, 192)));
		
		saveButton = new JButton("保存");
		saveButton.addActionListener(new SaveButtonAction());
		southPanel.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
		
		testConnButton = new JButton("测试连接");
		testConnButton.addActionListener(new TestConnButtonAction());
		southPanel.add(testConnButton);
		southPanel.add(saveButton);
		
		clearButton = new JButton("清空");
		clearButton.addActionListener(new ClearButtonAction());
		southPanel.add(clearButton);

		add(southPanel, "cell 0 3,grow");

		initTextField();
	}
	
	private void initTextField() {
		List<ConfigVo> list = config.getConfigVos();
		if (list!=null && list.size()>0) {			
			fillTextField(list.get(0));
		}
	}

	private DefaultComboBoxModel<ConfigVo> initCombox() {
		List<ConfigVo> list = config.getConfigVos();
		Vector<ConfigVo> v = new Vector<ConfigVo>();
		v.addAll(list);
		dcbm = new DefaultComboBoxModel<ConfigVo>(v);
		return dcbm;
	}
	
	private class SaveButtonAction implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			String configName = configTextField.getText();
			if (StringUtils.isBlank(configName)) {
				return;
			}
			ConfigVo configVo = null;
			List<ConfigVo> list = config.getConfigVos();
			for (ConfigVo cv : list) {
				if (configName.equals(cv.getName())) {
					configVo = cv;
					break;
				}
			}
			
			if (configVo == null) {
				configVo = new ConfigVo();
				configVo.setName(configName);
				list.add(configVo);
				initCombox();
				configComboBox.setModel(initCombox());
//				configComboBox.setSelectedItem(configVo);
			}
			
			Config conf = new Config();
			conf.setDatabaseName(databaseNameTextField.getText());
			conf.setBasePackage(basePackageTextField.getText());
			conf.setBeanPackage(beanPackageTextField.getText());
			conf.setDaoPackage(daoPackageTextField.getText());
			conf.setMapperPackage(mapperPackageTextField.getText());
			conf.setServicePackage(servicePackageTextField.getText());
			conf.setControllerPackage(controllerPackageTextField.getText());
			conf.setEntityPackage(entityPackageTextField.getText());
			conf.setEnumPackage(enumPackageTextField.getText());
			conf.setTables(tablesTextField.getText());
			conf.setNeedModules(needModulesTextField.getText());
			conf.setPath(pathTextField.getText());
			conf.setDriver(driverTextField.getText());
			conf.setUrl(urlTextField.getText());
			conf.setUsername(usernameTextField.getText());
			conf.setPassword(passwordTextField.getText());
			
			configVo.setConfig(conf);
			configVo.setIsDefault(isDefaultYesRadioButton.isSelected() ? 1 : 0);
			
			config.saveToJson();
		}
	}
	
	private class ClearButtonAction implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			// TODO 所有输入框中设置为Combox中选中的配置的值
		}
	}
	
	private class ConfigComboBoxItem implements ItemListener {
		public void itemStateChanged(ItemEvent e) {
			if (e.getStateChange()==ItemEvent.SELECTED) {				
				ConfigVo configVo = (ConfigVo) dcbm.getSelectedItem();
				fillTextField(configVo);
			}
		}
	}
	private class TestConnButtonAction implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			boolean ret = DBHelper.getInstance().getConnection(driverTextField.getText(), urlTextField.getText(), usernameTextField.getText(), passwordTextField.getText());
			if (ret) {
				JOptionPane.showMessageDialog(null, "恭喜，连接成功", "提示", JOptionPane.INFORMATION_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(null, "连接失败，请检查", "提示", JOptionPane.ERROR_MESSAGE);
            }
		}
	}
	
	private void fillTextField(ConfigVo configVo) {
		configTextField.setText(configVo.getName());
		Config conf = configVo.getConfig();
		databaseNameTextField.setText(conf.getDatabaseName());
		basePackageTextField.setText(conf.getBasePackage());
		beanPackageTextField.setText(conf.getBeanPackage());
		daoPackageTextField.setText(conf.getDaoPackage());
		mapperPackageTextField.setText(conf.getMapperPackage());
		servicePackageTextField.setText(conf.getServicePackage());
		controllerPackageTextField.setText(conf.getControllerPackage());
		entityPackageTextField.setText(conf.getEntityPackage());
		enumPackageTextField.setText(conf.getEnumPackage());
		tablesTextField.setText(conf.getTables());
		needModulesTextField.setText(conf.getNeedModules());
		pathTextField.setText(conf.getPath());
		
		driverTextField.setText(conf.getDriver());
		urlTextField.setText(conf.getUrl());
		usernameTextField.setText(conf.getUsername());
		passwordTextField.setText(conf.getPassword());
		
		if (configVo.getIsDefault()==ConfigVo.IS_DEFAULT_YES) {
			isDefaultYesRadioButton.setSelected(true);
		} else {
			isDefaultNoRadioButton.setSelected(true);
		}
	}
}
